package com.yike.user.service.impl;

import com.yike.user.entity.PlatformMenu;
import com.yike.user.entity.PlatformRoleMenu;
import com.yike.user.mapper.PlatformMenuMapper;
import com.yike.user.mapper.PlatformRoleMenuMapper;
import com.yike.user.param.PlatformRoleMenuParam;
import com.yike.user.service.PlatformRoleMenuService;
import com.yike.user.param.PlatformRoleMenuPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.utils.PlatformMenuUtil;
import com.yike.user.vo.PlatformMenuTreeVo;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 平台角色菜单 服务实现类
 *
 * @author Sakura
 * @since 2024-10-29
 */
@Slf4j
@Service
public class PlatformRoleMenuServiceImpl extends BaseServiceImpl<PlatformRoleMenuMapper, PlatformRoleMenu> implements PlatformRoleMenuService {

    @Autowired
    private PlatformRoleMenuMapper platformRoleMenuMapper;
    @Autowired
    private PlatformMenuMapper platformMenuMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(PlatformRoleMenuParam platformRoleMenuParam) throws Exception {
        // 如果权限为空则表示清空权限
        if (platformRoleMenuParam.getMenuIds() == null || platformRoleMenuParam.getMenuIds().size() < 1) {
            platformRoleMenuMapper.deleteByRoleId(platformRoleMenuParam.getRoleId());
            return true;
        }

        // 先获取角色原有权限信息
        Set<Long> menuIds = platformRoleMenuMapper.findMenuIdsByRoleId(platformRoleMenuParam.getRoleId());
        // 如果角色原先就有权限则先处理原有权限
        if (menuIds != null && menuIds.size() > 0) {
            // 找出新增的权限
            Set<Long> newAddMenus = platformRoleMenuParam.getMenuIds().stream()
                    .filter(element -> !menuIds.contains(element))
                    .collect(Collectors.toSet());
            if (newAddMenus.size() > 0) {
                platformRoleMenuMapper.saveRoleMenus(platformRoleMenuParam.getRoleId(), newAddMenus);
            }
            // 找出删除的权限
            Set<Long> deleteMenus = menuIds.stream()
                    .filter(element -> !platformRoleMenuParam.getMenuIds().contains(element))
                    .collect(Collectors.toSet());
            if (deleteMenus.size() > 0) {
                platformRoleMenuMapper.deleteByMenuIds(platformRoleMenuParam.getRoleId(), deleteMenus);
            }
        } else {
            platformRoleMenuMapper.saveRoleMenus(platformRoleMenuParam.getRoleId(), platformRoleMenuParam.getMenuIds());
        }
        return true;
    }

    @Override
    public List<PlatformMenuTreeVo> getMenus(Long roleId) {
        // 获取角色所有权限
        List<PlatformMenu> platformMenus = platformMenuMapper.getPlatformMenus(roleId);
        return PlatformMenuUtil.getPlatformMenuTreeVos(platformMenus);
    }

    @Override
    public List<Long> getMenuIds(Long roleId) {
        return platformMenuMapper.getMenuIds(roleId);
    }

}
